package gae.google.tcserver.servlets;

import gae.google.tcserver.api.CreateJson;
import gae.google.tcserver.db.GetFromDB;
import gae.google.tcserver.db.User;

import java.io.IOException;
//import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

/**
 * The <tt>LoginServlet</tt> class checks if a logged in user
 * is authenticated or not. If so, a session is created and 
 * the user key id is saved as session attribute. 
 *
 * @author Lars Friberg
 *
 * @date 2011-01-15
 */
public class LoginServlet extends HttpServlet {

//    private static final Logger log = Logger.getLogger(LoginServlet.class.getName());

	/**
	 * Serial version Id for serializability
	 */
	private static final long serialVersionUID = -147978042018929356L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// save all parameters received from RegisterUser.jsp
		String userName = request.getParameter("userName");
		String userPassword = request.getParameter("userPassword");
		User user = null;
		
		HttpSession session = request.getSession(false);
		if (userName.contains("administrator") && userPassword.contains("abc123")){
			// save administrator as user key id
			Key adminKey = KeyFactory.createKey("administrator", 123);
			
			// create session
			session = request.getSession(true);
			session.setAttribute("userkeyid", adminKey);
		}else{
			if (session.getAttribute("userkeyid") == null){
				
				// is user authenticated
				user = GetFromDB.getUser(userName, userPassword);
				
				// authenticated user
				if (user != null) {
					// yes, save user key id as an indication of signed in user
					session.setAttribute("userkeyid", user.getUserKeyId());
				}else{
					// dispatch to Login.jsp
					RequestDispatcher view = getServletContext().getRequestDispatcher("/views/Login.jsp");
					view.forward(request, response);
				}
			}
		}
		
		// get all teams a JSON
		CreateJson createJson = new CreateJson();
		String json = createJson.getTeams();
		request.setAttribute("json", json);

		// dispatch to TeamCalendar.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/TeamCalendar.jsp");
		view.forward(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException
	{
		// dispatch to Poll.jsp
		RequestDispatcher view = getServletContext().getRequestDispatcher("/views/Login.jsp");
		view.forward(request, response);
	}

}